package com.ctgu.springstart.controller;

import com.ctgu.springstart.domain.JsonData;
import com.ctgu.springstart.domain.User;
import com.ctgu.springstart.util.JsonUtils;
import com.ctgu.springstart.util.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/redis")
public class RedisTestController {

    @Autowired
    private RedisClient redis;

    @GetMapping("add")
    public Object add(String key, String name){
        redis.set(key, name);
        return JsonData.builder().msg("add name").build();
    }

    @GetMapping("get")
    public Object get(String key){
        String value = redis.get(key);
        return JsonData.builder().data(value).msg("get name").build();
    }

    @GetMapping("save_user")
    public Object saveUser(){
        User user = new User(1, "ct", 20,
                "1234567","1137191942@qq.com");
        String userStr = JsonUtils.obj2String(user);
        boolean flag = redis.set("base:user:11", userStr);
        return JsonData.builder().data(flag).msg("get name").build();
    }

    @GetMapping("get_user")
    public Object getUser(){
        String userStr = redis.get("base:user:11");
        User user = JsonUtils.string2Obj(userStr, User.class);
        return JsonData.builder().data(user).msg("get name").build();
    }



}
